package railRoad.worker;

import java.io.IOException;

import railRoad.Schedule;
import railRoad.db.DBReader;
import railRoad.db.H2Reader;
import railRoad.exception.OpenFileException;
import railRoad.exception.ReadException;
import railRoad.exception.ReaderException;
import railRoad.io.AbstractReaderWriter;
import railRoad.io.files.JSONReader;
import railRoad.io.files.XML;

//Singelton
public class WorkerClass{
	
	private static WorkerClass instance = new WorkerClass();
	
	private AbstractReaderWriter readerWriter = null;
	
	private WorkerClass() {
		super();
		Schedule.getInstance();
	}
	
	public static WorkerClass getInstance() {
		return instance;
	}
	
	public void initReader(String readerName)
	{
		//if(readerName.equals("Console"))	reader = new ConsoleReader();
		//if(readerName.equals("CSV"))		reader = new CSVReader(); 
		if(readerName.equals("JSON")) 		readerWriter = new JSONReader();
		//if(readerName.equals("YAML")) 	reader = new YAMLReader();
		if(readerName.equals("H2")) 		readerWriter = new H2Reader();
		if(readerName.equals("XML"))		readerWriter = new XML();
	}
	
	public void read(String in) throws ReaderException{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
			
		try {
				readerWriter.open(in, null);
				readerWriter.read();
				//passangerSimulation();
			}//try
			catch (OpenFileException exc) {
				System.out.println(exc.getMessage());
			}//catch
			catch(ReadException exc)
			{
				System.out.println(exc.getMessage());
			}//catch
			catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println("Some magic exception");		
			}//catch
	}
	
	public void write(String out) throws ReaderException {
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
			
		try {
				readerWriter.open(null, out);
				readerWriter.write();
				//passangerSimulation();
			}//try
			catch (OpenFileException exc) {
				System.out.println(exc.getMessage());
			}//catch
			catch(ReadException exc)
			{
				System.out.println(exc.getMessage());
			}//catch
			catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println("Some magic exception");		
			}//catch
	}
	
	public void  initDB(String login, String password) {
		try {
			readerWriter.open(login, password);
		}
		catch (OpenFileException e) {
			System.out.println(e.getMessage());
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void closeDB()
	{
		try {
			readerWriter.close();
		} catch (IOException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public void readDB() throws ReaderException {
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
			
		try {
				//readerWriter.open(login, password);
				readerWriter.read();
				//passangerSimulation();
			}//try
			catch (OpenFileException exc) {
				System.out.println(exc.getMessage());
			}//catch
			catch(ReadException exc)
			{
				System.out.println(exc.getMessage());
			}//catch
			catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println("Some magic exception");		
			}//catch
	}
	
	public void writeDB() throws ReaderException {
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
			
		try {
				//readerWriter.open(login, password);
				readerWriter.write();
				//passangerSimulation();
			}//try
			catch (OpenFileException exc) {
				System.out.println(exc.getMessage());
			}//catch
			catch(ReadException exc)
			{
				System.out.println(exc.getMessage());
			}//catch
			catch (Exception e) {
				System.out.println(e.getMessage());
				System.out.println("Some magic exception");		
			}//catch
			finally
			{
				try {
					readerWriter.close();
				} catch (IOException e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				} catch (Exception e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}//finally
	}

	public<T> void insert(T object, int parentId) throws ReaderException
	{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		
		try
		{
			((DBReader)readerWriter).insert(object,parentId);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	public<T> void update(T object) throws ReaderException
	{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		try
		{
			((DBReader)readerWriter).update(object);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public<T> void delete(T object) throws ReaderException
	{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		try
		{
			((DBReader)readerWriter).delete(object);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
	
	public<T> T select(T object, int id) throws ReaderException
	{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		
		T obj = null;
		
		try
		{
			obj = ((DBReader)readerWriter).select(object,id);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return obj;
	}
	
	public<T> T select(T object, String field) throws ReaderException
	{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		
		T obj = null;
		
		try
		{
			obj = ((DBReader)readerWriter).select(object,field);
		}
		catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return obj;
	}
	
	/*	
	public void insertStation(int route_id, Station station) throws ReaderException {
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		
		try {
			((DBReader)readerWriter).insertStation(route_id, station);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}//insertStation
	
	public void updateStation(int station_id, Station station) throws ReaderException {
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		
		try {
			((DBReader)readerWriter).updateStation(station_id, station);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}//updateStation
	
	public void deleteRoute(int route_id) throws ReaderException
	{
		if(readerWriter == null)
			throw new ReaderException("Can't create such reader");
		
		try {
			((DBReader)readerWriter).deleteRoute(route_id);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
*/
}//class WorkerClass
